package 力扣.双指针;

public class 拆分链表 {
    /**
     * 给定一个链表，需要把链表从中间拆分成长度相等的两半（如果链表长度为奇数，那么拆分之后，前半部分长度更长一点）。
     *
     * 输入：[1->2->3->4->5]
     * 输出：[1->2->3, 4->5]
     */
    private ListNode[] split(ListNode head){
        ListNode midList = findMidList(head);
        ListNode l2 = midList.next;
        midList.next = null;
        return new ListNode[]{midList,l2};
    }
    private ListNode findMidList(ListNode head){
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null){
            if (fast.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }else {
                break;
            }
        }
        return slow;
    }
    public ListNode middleNode(ListNode head) {
        ListNode dummy = new ListNode();
        dummy.next =head;
        ListNode fast = dummy;
        ListNode slow = dummy;
        while (fast != null){
            if (fast.next != null){//偶数个数
                fast = fast.next.next;
                slow = slow.next;
            }else {
                return slow.next;//奇数个数
            }
        }
        return slow;
    }
}
